package br.com.virtualsoluction.dao;

import br.com.virtualsoluction.bo.Venda;
import br.com.virtualsoluction.utilitarios.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


public class DAOVenda implements CRUD<Venda> {

    private String sql;
    private Connection c;
    private PreparedStatement s;
    private ResultSet rs;
    
    public DAOVenda(){
        this.sql = "";
        this.c = new ConnectionFactory().getConnection();
        this.s = null;
        this.rs = null;        
    } 
    
    public boolean insert(Venda elemento) {
        int i;
        boolean b;
        elemento.setIdVenda(getChave());
        this.sql =  " insert into venda (id, data, status, cliente_id)";
        this.sql += " values ( "+elemento.getIdVenda()+" , "+elemento.getData()+" , 'E' , "+elemento.getCliente().getId()+")";

        

        try {
            this.s = this.c.prepareStatement(this.sql);           
            b = this.s.execute();
            return b;


        } catch (SQLException ex) {
            return false;
        }

        


    }

    public boolean update(Venda elemento) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean delete(Venda elemento) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public Venda search(int id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Venda> getList() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public int getChave(){
        
        int ret = 0;
        this.sql = "select gen_id(gen_venda_id,1) from rdb$database";
        try {
            this.s = this.c.prepareStatement(this.sql);
        } catch (SQLException ex) {
            Logger.getLogger(DAOVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            this.rs = this.s.executeQuery();
        } catch (SQLException ex) {
            Logger.getLogger(DAOVenda.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            rs.next();
            ret = rs.getInt("gen_id");
        } catch (SQLException ex) {
            Logger.getLogger(DAOVenda.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ret;
    }
}